use crate::ApiError;
use axum::extract::FromRequestParts;

/// 通过[axum::extract::Query]提取器实现自己的提取器
///
/// 可以返回自定义的数据结构和状态码
#[derive(Debug, FromRequestParts)]
#[from_request(via(axum::extract::Query), rejection(ApiError))]
pub struct Query<T>(pub T);

// impl<S, T> FromRequestParts<S> for Query<T>
// where
//     axum::extract::Query<T>: FromRequestParts<S>,
//     Error: From<<axum::extract::Query<T> as FromRequestParts<S>>::Rejection>,
//     S: Send + Sync,
// {
//     type Rejection = ApiError;
//     async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
//         <axum::extract::Query<T> as ::axum::extract::FromRequestParts<_>>::from_request_parts(
//             parts, state,
//         )
//         .await
//         .map(|axum::extract::Query(value)| Query(value))
//         .map_err(From::from)
//     }
// }


/// 简化[Valid<Query<T>>]的提取器
#[derive(Debug)]
pub struct ValidQuery<T>(pub T);

// impl<S, T> axum::extract::FromRequestParts<S> for ValidQuery<T>
// where
//     T: ::serde::de::DeserializeOwned + ::validator::Validate,
//     S: Sync + Send,
//     Query<T>: axum::extract::FromRequestParts<S, Rejection = ApiError>,
// {
//     type Rejection = ApiError;
// 
//     async fn from_request_parts(
//         parts: &mut axum::http::request::Parts,
//         state: &S,
//     ) -> Result<Self, Self::Rejection> {
//         let Query(v) = Query::<T>::from_request_parts(parts, state).await?;
//         v.validate()?;
//         Ok(ValidQuery(v))
//     }
// }
